"""
    @Author  ：思念 
    @File    ：11.小红书自动评论脚本.py
    @Date    ：2025/4/9 16:31 
"""

import time
from DrissionPage import ChromiumPage

search = str(input("输入关键词："))
content = str(input("输入评论内容："))

page = ChromiumPage()
page.get('https://www.xiaohongshu.com/search_result?keyword=' + search + '&source=unknown&type=51')
print("网站加载成功！")
time.sleep(2)
for time_button in range(1, 20):  # 下滑多少下 你就改多少下
    time.sleep(2)
    page.scroll.to_bottom()
    print("当前下滑：", time_button, "次，剩余", 20 - time_button, "次后，将会开始抓取数据...")
print("全部下滑完毕开始抓取页面的元素链接！")
my_list = list()
ele = page.eles('.cover ld mask')
name_ele = page.eles('.title')
for href, name in zip(ele, name_ele):
    lian = href.link
    na = name.text
    print(na, lian)
    my_list.extend(lian.split(','))

sums = 0

print("本次获取数据：", len(my_list), "条")
for like_list in my_list:
    sums = sums + 1
    print("序号：", sums, "链接：", like_list)
    page.get(like_list)
    time.sleep(1)
    input_list1 = page.ele('.chat-wrapper').click()
    input_list2 = page.ele('.content-input').input(content)
    time.sleep(0.5)
    button = page.ele('.btn submit').click()
    print("发送成功：", content, "-")
    time.sleep(2)
print("*" * 30)
input("主程序结束...")
